# 自动取款机
'''
    1.登录
    2.退出
    3.查询余额
    4.取钱
    5.存钱
'''
# 初始主界面
def mainGui():
    print('='*10, end='')
    print('{0:^10}'.format('自动取款机'), end='')
    print('='*10)
    print('{0:^13}{1:^13}'.format('1.登录','2.查询余额'))
    print('-' * 34)
    print('{0:^13}{1:^13}'.format('3.取钱', '4.存钱  '))
    print('-' * 34)
    print('{0:^26}'.format('5.退出系统'))
    print('='*35)
    op = input('请输入相应的选择：')
    return op


# 1.登录
def login(userinfo, user):
    name = input('请输入用户名： ')
    password = input('请输入密码： ')
    for i, n in enumerate(userinfo):
        if n['name'] == name and n['password'] == password:
            user.append(n)
    if len(user) == 0:
        print('用户不存在或密码错误！')
    else:
        print('=' * 10, end='')
        print('{0:^10}'.format('登录成功'), end='')
        print('=' * 10)
    return user



# 2.查询余额
def getbalance(user):
    print('{}:{}'.format('您的余额为：', user[0]['money']))

# 3.取钱
def drawoney(user):
    usermoney = int(input('请输入您取钱的金额： '))
    if usermoney <= user[0]['money']:
        user[0]['money'] = user[0]['money'] - usermoney
        print('{}{}'.format('钱已取出，当前账户余额为： ',user[0]['money']))
    else:
        print('您账户余额不足')
    return user

# 4.存钱
def saveMoney(userinfo):
    usermoney = int(input('请输入您存钱的金额： '))
    user[0]['money'] = user[0]['money'] + usermoney
    print('{}{}'.format('钱已存入，当前账户余额为： ', user[0]['money']))
    return user

# 5.退出
def logout():
    print('=' * 10, end='')
    print('{0:^10}'.format('谢谢使用，再见！'), end='')
    print('=' * 10)


userinfo = [{'name':'小马','password':'12345','money':1000},{'name':'小王','password':'12345','money':900},
            {'name':'小李','password':'12345','money':400},{'name':'小张','password':'12345','money':0}]
user = []
# 总控函数
def MyATM(userinfo, user):
    while True:
        op = mainGui()
        if op == '1':
            user = login(userinfo,user)
        elif op == '2':
            if len(user) != 0:
                getbalance(user)
            else:
                print('请先完成登录！')
        elif op == '3':
            if len(user) != 0:
                user = drawoney(user)
                for i, n in enumerate(userinfo):
                    if n['name'] == user[0]['name']:
                        userinfo[i] = user[0]
            else:
                print('请先完成登录！')
        elif op == '4':
            if len(user) != 0:
                user = saveMoney(user)
                for i, n in enumerate(userinfo):
                    if n['name'] == user[0]['name']:
                        userinfo[i] = user[0]
            else:
                print('请先完成登录！')
        elif op == '5':
            logout()
            break
        else:
            print('输入有误')
        input('输入任意键继续！')

MyATM(userinfo,user)